/**
 * 
 */
package de.karlNet.executor.gui;

import java.io.IOException;
import java.security.InvalidParameterException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.fieldassist.AutoCompleteField;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import de.karlNet.executor.controller.Controller;

/**
 * @author Tom
 * 
 */
public class ExecGui {
	private Controller controller = new Controller();
	private Text text;

	/**
	 * @param args
	 * @wbp.parser.entryPoint
	 */
	public void show() {

		Display display = new Display();

		Shell shell = new Shell(display);
		shell.setSize(513, 70);
		shell.setLayout(new GridLayout(2, false));

		text = new Text(shell, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		text.addKeyListener(new KeyListener() {

			public void keyReleased(KeyEvent arg0) {
				if (arg0.keyCode == SWT.CR || arg0.keyCode == SWT.LF) {
					try {
						controller.exec(text.getText());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if (arg0.keyCode == SWT.ESC) {
					System.exit(0);
				}
			}

			public void keyPressed(KeyEvent arg0) {
			}
		});
		final AutoCompleteField autoCompleteField = new AutoCompleteField(text,
				new TextContentAdapter(), this.controller.getApps());
		controller.scanForApplicatons();
		autoCompleteField.setProposals(controller.getApps());
		Button btnRun = new Button(shell, SWT.NONE);
		btnRun.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					controller.exec(text.getText());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (InvalidParameterException e) {
					Status status = new Status(IStatus.ERROR, "My Plug-in ID",
							0, "You have to type in a valid program name", null);
					ErrorDialog.openError(
							Display.getCurrent().getActiveShell(),
							"JFace Error", e.getMessage(), status);
				}
			}
		});
		btnRun.setText("Run");

		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();

		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;

		shell.setLocation(x, y);

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

}